<template>
    <div>
        <div style="margin: 10px 0">
            <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search"
                      v-model="fileName"></el-input>
            <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>-->
            <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-position" class="ml-5" v-model="address"></el-input>-->
            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>

        <div style="margin: 10px 0">
            <el-button type="primary" @click="dialogUpload = true">上传文件 <i class="el-icon-upload"></i></el-button>

            <!--            <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>-->
            <el-popconfirm
                    class="ml-5"
                    confirm-button-text='确定'
                    cancel-button-text='我再想想'
                    icon="el-icon-info"
                    icon-color="red"
                    title="您确定批量删除这些数据吗？"
                    @confirm="delBatch">
                <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
            <!-- <el-upload action="http://localhost:9090/raceFile/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">
              <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>
            </el-upload>
            <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button> -->
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"
                  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <!--<el-table-column prop="id" label="ID" width="80" sortable></el-table-column>-->
            <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
            <el-table-column prop="fileMd5" label="文件唯一标识" align="center"></el-table-column>
            <el-table-column prop="fileName" label="文件名" align="center"></el-table-column>
            <el-table-column prop="fileSize" label="文件大小(MB)" align="center">
                <template slot-scope="scope">
                    <span>{{ scope.row.fileSize }}MB</span>
                </template>
            </el-table-column>
            <el-table-column prop="fileType" label="文件类型" align="center"></el-table-column>
            <el-table-column prop="fileUrl" label="文件路径" align="center"></el-table-column>
            <el-table-column prop="status" v-if="false" label="状态" align="center"></el-table-column>

            <el-table-column label="操作" width="180" align="center">
                <template slot-scope="scope">
<!--                    <el-tooltip class="item" effect="dark" content="编辑信息" placement="top-start">-->
<!--                        <el-button type="text" @click="handleEdit(scope.row)"><i class="el-icon el-icon-edit"></i>-->
<!--                        </el-button>-->
<!--                    </el-tooltip>-->
                    <el-tooltip class="item" effect="dark" content="删除" placement="top-start">
                        <el-popconfirm
                                class="ml-5"
                                confirm-button-text='确定'
                                cancel-button-text='我再想想'
                                icon="el-icon-info"
                                icon-color="red"
                                title="您确定删除吗？"
                                @confirm="del(scope.row.fileMd5)"
                        >
                            <el-button type="text" slot="reference"><i class="el-icon el-icon-delete"></i></el-button>
                        </el-popconfirm>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <div style="padding: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[10, 20, 50, 100]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <el-dialog title="信息" :visible.sync="dialogFormVisible" width="40%" :close-on-click-modal="false">
            <el-form label-width="120px" size="small" style="width: 80%; margin: 0 auto">
                <el-form-item prop="fileMd5" label="文件唯一标识">
                    <el-input v-model="form.fileMd5" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="fileName" label="文件名">
                    <el-input v-model="form.fileName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="fileSize" label="文件大小">
                    <el-input v-model="form.fileSize" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="fileType" label="文件类型">
                    <el-input v-model="form.fileType" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="fileUrl" label="文件路径">
                    <el-input v-model="form.fileUrl" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="status" v-if="false" label="状态">
                    <el-input v-model="form.status" autocomplete="off"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog title="上传文件" :visible.sync="dialogUpload" width="40%" :close-on-click-modal="false">
            <el-form label-width="120px" size="small" style="width: 80%; margin: 0 auto">
                <el-upload ref="upload" class="upload" drag multiple
                           :limit="3"
                           action=""
                           :auto-upload="false"
                           :on-remove="handleRemove"
                           :on-exceed="handleExceed"
                           :on-change="handleChange"
                           :show-file-list="true"
                           :file-list="newFileList"
                >
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                    <div class="el-upload__tip" slot="tip">只能上传不超过10Mb大小文件</div>
                </el-upload>
                <el-table :data="uploadFiles" border stripe :header-cell-class-name="'headerBg'">
                    <el-table-column prop="fileName" label="文件名" align="center"></el-table-column>
                    <el-table-column prop="fileSize" label="文件大小" align="center">
                        <template slot-scope="scope">
                            <span>{{ scope.row.fileSize.toFixed(2) }}MB</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="fileType" label="文件类型" align="center"></el-table-column>
                </el-table>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="confirmUpload">确 定</el-button>
                <el-button @click="close()">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "RaceFile",
        data() {
            return {
                tableData: [],
                total: 0,
                pageNum: 1,
                pageSize: 10,
                fileName: "",
                form: {},
                dialogFormVisible: false,
                dialogUpload: false,
                multipleSelection: [],
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
                uploadUrl: this.GLOBAL.baseURL + 'rank/file/upload',
                newFileList: [],
                uploadFiles:[],
                uploadFile:{
                    fileName:'',
                    fileType:'',
                    fileSize:0
                }
            }
        },
        created() {
            this.load()
        },
        methods: {
            load() {
                const data = {
                    "page": this.pageNum,
                    "pageSize": this.pageSize,
                    "fileName": this.fileName
                }
                this.request.post("/raceFile/page", data).then(res => {
                    //this.tableData = res.data.records
                    this.tableData = res.data.data
                    this.total = res.data.total
                })
            },
            handleChange(file,fileList) {
                const files = []
                this.newFileList = fileList
                for (let i = 0; i < fileList.length ; i++) {
                    this.uploadFile.fileName = fileList[i].name
                    this.uploadFile.fileType = fileList[i].name.split(".")[1]
                    this.uploadFile.fileSize = fileList[i].size/(1024*1024)
                    if(this.uploadFile.fileSize>10){
                        this.newFileList=[]
                        return this.$message.error(this.uploadFile.fileName+"文件过大，无法上传")
                    }
                    files.push(this.uploadFile)
                    this.uploadFile={}
                }
                this.uploadFiles = files
            },
            handleExceed(files, fileList) {
                this.$message.warning('最多上传3个文件！');
            },
            // 删除附件
            handleRemove(file, fileList) {
                this.uploadFiles= []
                this.newFileList = fileList;
                this.newFileList.forEach(item => {
                    this.uploadFile.fileName = item.name
                    this.uploadFile.fileType = item.name.split(".")[1]
                    this.uploadFile.fileSize = item.size/(1024*1024)
                    this.uploadFiles.push(this.uploadFile)
                })
            },
            confirmUpload() {
                const formData = new FormData()
                let config ={
                    headers:{'Content-Type':'multipart/form-data'}
                }
                this.newFileList.forEach(item => {
                    formData.append('files', item.raw)
                    console.log(item.raw, item)
                })
                this.request.post("/raceFile/upload",formData,config).then(res =>{
                    if (res.code === 200) {
                        this.$message.success(res.message)
                        this.dialogUpload = false
                        this.uploadFiles=[]
                        this.newFileList=[]
                        this.uploadFile={}
                        this.load()
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            save() {
                this.request.post("/raceFile/save", this.form).then(res => {
                    if (res.code === 200) {
                        this.$message.success(res.message)
                        this.dialogFormVisible = false
                        this.load()
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            close(){
                this.dialogUpload = false;
                this.uploadFiles=[]
                this.uploadFile={}
                this.newFileList=[]
            },
            handleAdd() {
                this.dialogFormVisible = true
                this.form = {}
                this.$nextTick(() => {
                    if (this.$refs.img) {
                        this.$refs.img.clearFiles();
                    }
                    if (this.$refs.file) {
                        this.$refs.file.clearFiles();
                    }
                })
            },
            handleEdit(row) {
                this.form = JSON.parse(JSON.stringify(row))
                this.dialogFormVisible = true
                this.$nextTick(() => {
                    if (this.$refs.img) {
                        this.$refs.img.clearFiles();
                    }
                    if (this.$refs.file) {
                        this.$refs.file.clearFiles();
                    }
                })
            },
            del(md5) {
                const data = {
                    "md5":md5
                }
                this.request.post("/raceFile/deleteByMd5",data).then(res => {
                    if (res.code === 200) {
                        this.$message.success("删除成功")
                        this.load()
                    } else {
                        this.$message.error("删除失败")
                    }
                })
            },
            handleSelectionChange(val) {
                console.log(val)
                this.multipleSelection = val
            },
            delBatch() {
                if (!this.multipleSelection.length) {
                    this.$message.error("请选择需要删除的数据")
                    return
                }
                let ids = this.multipleSelection.map(v => v.fileMd5)  // [{}, {}, {}] => [1,2,3]
                this.request.post("/raceFile/del/batch", ids).then(res => {
                    if (res.code === 200) {
                        this.$message.success("批量删除成功")
                        this.load()
                    } else {
                        this.$message.error("批量删除失败")
                    }
                })
            },
            reset() {
                this.fileName = ""
                this.load()
            },
            handleSizeChange(pageSize) {
                console.log(pageSize)
                this.pageSize = pageSize
                this.load()
            },
            handleCurrentChange(pageNum) {
                console.log(pageNum)
                this.pageNum = pageNum
                this.load()
            },
            handleFileUploadSuccess(res) {
                this.form.file = res
            },
            handleImgUploadSuccess(res) {
                this.form.img = res
            },
            download(url) {
                window.open(url)
            },
            exp() {
                window.open("http://localhost:9090/raceFile/export")
            },
            handleExcelImportSuccess() {
                this.$message.success("导入成功")
                this.load()
            }
        }
    }
</script>


<style>
    .headerBg {
        background: #eee !important;
    }

    .el-button {
        margin: 0px 6px 12px;
    }

    .el-icon {
        font-size: 16px;
    }

</style>
